﻿using Microsoft.AspNetCore.Mvc;
using MiniExcelLibs;
using ZR.Admin.WebApi.Filters;
using ZR.Model.Dto.Business;
using ZR.Model.Dto.Business.ProductionControl;
using ZR.Model.Models.ProductionControl;
using ZR.Service.IService.ProductionControl;

//创建时间：2025-02-23
namespace ZR.Admin.WebApi.Controllers
{
    /// <summary>
    /// 子篮信息
    /// </summary>
    [Verify]
    [Route("business/CksBasket")]
    [ApiExplorerSettings(GroupName = "business")]
    public class CksBasketController : BaseController
    {
        /// <summary>
        /// 子篮信息接口
        /// </summary>
        private readonly ICksBasketService _CksBasketService;

        public CksBasketController(ICksBasketService CksBasketService)
        {
            _CksBasketService = CksBasketService;
        }

        /// <summary>
        /// 查询子篮信息列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        [HttpGet("list")]
        [ActionPermissionFilter(Permission = "cksbasket:list")]
        public IActionResult QueryCksBasket([FromQuery] CksBasketQueryDto parm)
        {
            var response = _CksBasketService.GetList(parm);
            return SUCCESS(response);
        }


        /// <summary>
        /// 查询子篮信息详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpGet("{Id}")]
        [ActionPermissionFilter(Permission = "cksbasket:query")]
        public IActionResult GetCksBasket(long Id)
        {
            var response = _CksBasketService.GetInfo(Id);

            var info = response.Adapt<CksBasketDto>();
            return SUCCESS(info);
        }

        /// <summary>
        /// 添加子篮信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [ActionPermissionFilter(Permission = "cksbasket:add")]
        [Log(Title = "子篮信息", BusinessType = BusinessType.INSERT)]
        public IActionResult AddCksBasket([FromBody] CksBasketDto parm)
        {
            var modal = parm.Adapt<CksBasket>().ToCreate(HttpContext);

            var response = _CksBasketService.AddCksBasket(modal);

            return SUCCESS(response);
        }

        /// <summary>
        /// 更新子篮信息
        /// </summary>
        /// <returns></returns>
        [HttpPut]
        [ActionPermissionFilter(Permission = "cksbasket:edit")]
        [Log(Title = "子篮信息", BusinessType = BusinessType.UPDATE)]
        public IActionResult UpdateCksBasket([FromBody] CksBasketDto parm)
        {
            var modal = parm.Adapt<CksBasket>().ToUpdate(HttpContext);
            var response = _CksBasketService.UpdateCksBasket(modal);

            return ToResponse(response);
        }

        /// <summary>
        /// 删除子篮信息
        /// </summary>
        /// <returns></returns>
        [HttpDelete("delete/{ids}")]
        [ActionPermissionFilter(Permission = "cksbasket:delete")]
        [Log(Title = "子篮信息", BusinessType = BusinessType.DELETE)]
        public IActionResult DeleteCksBasket([FromRoute] string ids)
        {
            var idArr = Tools.SplitAndConvert<long>(ids);

            return ToResponse(_CksBasketService.Delete(idArr));
        }

        /// <summary>
        /// 下载子篮模版
        /// </summary>
        /// <returns></returns>
        [HttpGet("downloadTemplate")]
        [Log(Title = "下载子篮模版", BusinessType = BusinessType.EXPORT, IsSaveRequestData = true, IsSaveResponseData = false)]
        [AllowAnonymous]
        public IActionResult DownloadTemplateExcel()
        {
            (string, string) result = DownloadImportTemplate("basket"); 
            return ExportExcel(result.Item2, result.Item1);
        }

        /// <summary>
        /// 导入子篮数据
        /// </summary>
        /// <param name="formFile">使用IFromFile必须使用name属性否则获取不到文件</param>
        /// <returns></returns>
        [HttpPost("importData")]
        [Log(Title = "订单导入", BusinessType = BusinessType.IMPORT, IsSaveRequestData = false, IsSaveResponseData = true)]
        [ActionPermissionFilter(Permission = "cksbasket:edit")]
        public IActionResult ImportData([FromForm(Name = "file")] IFormFile formFile)
        {
            List<BasketSheet> orders = new();
            using (var stream = formFile.OpenReadStream())
            {
                orders = stream.Query<BasketSheet>(startCell: "A1").ToList();
            }

            return SUCCESS(null);
        }

    }
}